package com.cgc.venus.server.user.controller;
import com.cgc.venus.server.core.bean.response.VenusResponse;
import com.cgc.venus.server.core.bean.response.VenusResponseStatus;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.user.bean.*;
import com.cgc.venus.server.user.inter.TeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * Created by Administrator on 2017/6/3.
 */
@Api(value = "团队管理",description="团队管理所对应服务的api")
@RequestMapping("/team-admin")
@RestController
public class TeamController {
    @Autowired
    private TeamService teamServiceImpl;

    @ApiOperation("团队查询")
    @RequestMapping(value = "/team", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List<Team>> teamsQuery(@RequestParam("queryKey") String queryKey,@RequestParam("currentUserId") String currentUserId,@RequestParam("superAdmin") boolean superAdmin) throws VenusException {
        List<Team> teams=teamServiceImpl.teamQuery(queryKey, currentUserId, superAdmin);
        VenusResponse<List<Team>> response=new VenusResponse<List<Team>>(VenusResponseStatus.OK,"",teams);
        return response;
    }

    @ApiOperation("团队查询-应用监控/日志查询过滤使用")
     @RequestMapping(value = "/team1", method = RequestMethod.GET)
     @ResponseBody
     public VenusResponse<List> teamsQuery1(@RequestParam("currentUserId") String currentUserId,@RequestParam("superAdmin") boolean superAdmin) throws VenusException {
        List teams=teamServiceImpl.teamQuery(currentUserId, superAdmin);
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",teams);
        return response;
    }

    @ApiOperation("团队空间查询-应用监控/日志查询过滤使用")
    @RequestMapping(value = "/team1/{team_id}/namespace", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List> namespaceQuery1(@PathVariable("team_id") String teamId,@RequestParam("currentUserId") String currentUserId,@RequestParam("superAdmin") boolean superAdmin) throws VenusException {
        List namespaces=teamServiceImpl.namespaceQuery(currentUserId,teamId,superAdmin);
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",namespaces);
        return response;
    }

    @ApiOperation("团队新增")
    @RequestMapping(value = "/team/{current_user_id}", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> teamAdd(@RequestBody Team team,@PathVariable("current_user_id") String currentUserId) throws VenusException {
        boolean f=teamServiceImpl.teamAdd(team, currentUserId);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"新增成功",f);
        return response;
    }

    @ApiOperation("团队修改")
    @RequestMapping(value = "/team", method = RequestMethod.PATCH)
    @ResponseBody
    public VenusResponse<Boolean> teamUpdate(@RequestBody Team team) throws VenusException {
        boolean f=teamServiceImpl.teamUpdate(team);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"修改成功",f);
        return response;
    }


    @ApiOperation("团队停用")
    @RequestMapping(value = "/team/{team_id}", method = RequestMethod.DELETE)
    @ResponseBody
    public VenusResponse<Boolean> teamDisable(@PathVariable("team_id") String teamId) throws VenusException {
        boolean r= teamServiceImpl.teamDisableById(teamId);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"停用成功",r);
        return response;
    }

    @ApiOperation("团队明细查询")
    @RequestMapping(value = "/team/{team_id}", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<Team> teamDetailQuery(@PathVariable("team_id") String teamId) throws VenusException {
        Team team=teamServiceImpl.teamDetailQuery(teamId);
        VenusResponse<Team> response=new VenusResponse<Team>(VenusResponseStatus.OK,"",team);
        return response;
    }
    ///////////////////////////////////////////////////////////////////////////////////////////////////
    @ApiOperation("团队命名空间查询")
    @RequestMapping(value = "/team/{team_id}/namespace", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List<Namespace>> namespaceQuery(@PathVariable("team_id") String teamId) throws VenusException {
        List<Namespace> namespaces=teamServiceImpl.namespaceQuery(teamId);
        VenusResponse<List<Namespace>> response=new VenusResponse<List<Namespace>>(VenusResponseStatus.OK,"",namespaces);
        return response;
    }
    @ApiOperation("团队命名空间新增")
    @RequestMapping(value = "/team/{team_id}/namespace", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> namespaceAdd(@PathVariable("team_id") String teamId,@RequestBody Namespace namespace) throws VenusException {
        Boolean result = teamServiceImpl.namespaceAdd(teamId,namespace);
        VenusResponse<Boolean> response=null;
        if(result){
            response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"新增成功",result);
            // 新增镜像仓库的秘钥
            teamServiceImpl.createHarborSecret(namespace.getKubeName());
        }else{
            response=new VenusResponse<Boolean>(VenusResponseStatus.MYSQL_DB_EXCEPTION,"数据新增异常!",result);
        }
        return response;
    }

    @ApiOperation("团队命名空间停用")
    @RequestMapping(value = "/team/{team_id}/namespace", method = RequestMethod.DELETE)
    @ResponseBody
    public VenusResponse<Boolean> namespaceDisable(@PathVariable("team_id") String teamId,@RequestBody namespaceDisableVo namespaceIds) throws VenusException {
        Boolean result=teamServiceImpl.namespaceDisable(teamId,namespaceIds.getNamespaceIds());
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",result);
        return response;
    }

    ///////////////////////////////////////////////////////////////////////////////////////////////////
    @ApiOperation("命名空间新增用户")
    @RequestMapping(value = "/namespace/{namespace_Id}/user", method = RequestMethod.POST)
    @ResponseBody
    public VenusResponse<Boolean> namespaceUserAdd(@PathVariable("namespace_Id") String namespaceId,@RequestBody NamespaceOpeUser namespaceOpeUser) throws VenusException {
        boolean r=teamServiceImpl.namespaceAddUser(namespaceOpeUser.getNamespaceId(),namespaceOpeUser.getUserId());
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"用户添加成功",r);
        return response;
    }

    @ApiOperation("命名空间删除用户")
    @RequestMapping(value = "/namespace/{namespace_Id}/user", method = RequestMethod.DELETE)
    @ResponseBody
    public VenusResponse<Boolean> namespaceUserDelete(@PathVariable("namespace_Id") String namespaceId,@RequestBody NamespaceOpeUser namespaceOpeUser) throws VenusException {
        boolean r=teamServiceImpl.namespaceRemoveUser(namespaceOpeUser.getNamespaceId(), namespaceOpeUser.getUserId());
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"用户移除成功",r);
        return response;
    }

    @ApiOperation("命名空间用户(已选)查询")
    @RequestMapping(value = "/namespace/{namespace_Id}/user/select", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List<namespaceUser>> namespaceUserSelectQuery(@PathVariable("namespace_Id") String namespaceId) throws VenusException {
        List<namespaceUser> namespaceUsers=teamServiceImpl.namespaceUserSelectQuery(namespaceId);
        VenusResponse<List<namespaceUser>> response=new VenusResponse<List<namespaceUser>>(VenusResponseStatus.OK,"",namespaceUsers);
        return response;
    }

    @ApiOperation("命名空间用户(备选)查询")
    @RequestMapping(value = "/namespace/{namespace_Id}/user/un-select", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List> namespaceUserUnSelectQuery(@PathVariable("namespace_Id") String namespaceId) throws VenusException {
        List list= teamServiceImpl.namespaceUserUnSelectQuery(namespaceId);
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",list);
        return response;
    }

    @ApiOperation("命名空间物理分组设置")
    @RequestMapping(value = "/namespace/physical-group",method = RequestMethod.PATCH)
    @ResponseBody
    public VenusResponse<Boolean> teamNamespacePhysicalGroupSet(@RequestBody TeamNamespacePhysicalGroupSet tnpgs) throws VenusException {
        boolean r=teamServiceImpl.teamNamespacePhysicalGroupSet(tnpgs.getNamespaceIds(),tnpgs.getPhysicalGroupId());
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"设置成功",r);
        return response;
    }

    @ApiOperation("物理分组查询")
    @RequestMapping(value = "/physical-group",method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List> physicalGroupQuery() throws VenusException {
        List list= teamServiceImpl.physicalGroupQuery();
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",list);
        return response;
    }

    @ApiOperation("团队名称验证查询")
    @RequestMapping(value = "/validate/team-name/{type}/{name}", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<Boolean> validateTeamName(@PathVariable("type") String type,@PathVariable("name") String name) throws VenusException {
        boolean r= teamServiceImpl.validateTeamName(name,type);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",r);
        return response;
    }

    @ApiOperation("命名空间名称验证查询")
     @RequestMapping(value = "/validate/namespace-name/{type}/{name}", method = RequestMethod.GET)
     @ResponseBody
     public VenusResponse<Boolean> validateNamespaceName(@PathVariable("type") String type,@PathVariable("name") String name) throws VenusException {
        boolean r= teamServiceImpl.validateNamespaceName(name,type);
        VenusResponse<Boolean> response=new VenusResponse<Boolean>(VenusResponseStatus.OK,"",r);
        return response;
    }

    @ApiOperation("统计团队与命名空间")
    @RequestMapping(value = "/stats/teamAndNamespace", method = RequestMethod.GET)
    @ResponseBody
    public VenusResponse<List> teamAndNamespaceStats() throws VenusException {
        List list= teamServiceImpl.teamAndNamespaceStats();
        VenusResponse<List> response=new VenusResponse<List>(VenusResponseStatus.OK,"",list);
        return response;
    }
}